package club.dmyang.service;

import club.dmyang.common.enums.RespCodeEnums;
import club.dmyang.common.exception.HyBlogException;
import club.dmyang.common.vo.ResposeVo;
import club.dmyang.dao.CategoryMapper;
import club.dmyang.entity.Category;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by brss on 2018/7/14.
 */
@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {
    private static final Logger LOGGER = LoggerFactory.getLogger(CategoryServiceImpl.class);

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ArticleService articleService;

    /**
     * limit
     * @param page
     * @param limit 每页的容量
     * @return
     * start = (page-1)*limit
     * 每页容量=limit
     */
    @Override
    public List<Category> findByPageAndLimit(Integer page, Integer limit) {
        if ( page < 1 || limit < 1 ){
            LOGGER.error("page或limit错误！[ page = "+page + "limit = "+limit + "]");
            throw new HyBlogException(RespCodeEnums.GLOBAL_CODE_PARAMETER_ERROR);
        }
        List<Category> categories = categoryMapper.selectByPageAndLimit((page - 1) * limit, limit);
        return categories;
    }


    /**
     * 查找所有分类
     *
     * @return
     */
    @Override
    public List<Category> findAll() {
        return categoryMapper.findAll();
    }

    /**
     * 查询所有分类记录的个数
     * （用于分页计算页数 ）
     *
     * @return
     */
    @Override
    public int findtAllNums() {
        return categoryMapper.selectAllNums();
    }

    /**
     * 根据主键查询
     *
     * @param cid
     * @return
     */
    @Override
    public Category findByPrimaryKey(Integer cid) {
        return categoryMapper.selectByPrimaryKey(cid);
    }


}
